<?php
class TagsController extends AppController {

	var $name = 'Tags';

	function beforeFilter(){
		$this->belepesCheck();
		$this->setGeneralData();
		}

/*
 * ajaxos keresés a címkékben
*/
	function search(){
		$this->Task->recursive = 1;
		$this->set('searchResults', $this->Tag->findAll("tag LIKE '%{$this->data['Task']['tags']}%'",null,null, 10,1,1));
		$this->render('searchResults','ajax');
	}
	
/*
 * adott cimkéhez tartozó feladatok megjelenítése
 * @param int $id a cimke id-je
*/
	function lista($id = null){
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Tag.', true));
			$this->redirect('/');
		}
		$this->Tag->unbindModel(array('hasAndBelongsToMany' => array('Task')));
		$this->Tag->bindModel(array('hasAndBelongsToMany' => array(
																							'Task' => array(
																											'className' => 'Task',
																											'joinTable' => 'tags_tasks',
																											'foreignKey' => 'tag_id',
																											'associationForeignKey' => 'task_id',
																											'conditions' => 'Task.types_id != ' . $this->settings['task_type_finished'],
																											'fields' => array('Task.id'),
																											'order' => '',
																											'limit' => '',
																											'offset' => '',
																											'unique' => '',
																											'finderQuery' => '',
																											'deleteQuery' => '',
																											'insertQuery' => ''
																											)
																							)
													)
											);
		$tag = $this->Tag->find('Tag.id = ' . $id);
		$this->set('tag', $tag);
		$this->render('lista', 'ajax');
	}


	function index() {
		$this->Tag->recursive = 0;
		$this->set('tags', $this->Tag->findAll());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Tag.');
			$this->redirect('/tags/index');
		}
		$this->set('tag', $this->Tag->read(null, $id));
	}

	function add() {
		if (empty($this->data)) {
			$this->set('tasks', $this->Tag->Task->generateList());
			$this->set('selectedTasks', null);
			$this->render();
		} else {
			$this->cleanUpFields();
			if ($this->Tag->save($this->data)) {
				$this->Session->setFlash('The Tag has been saved');
				$this->redirect('/tags/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
				$this->set('tasks', $this->Tag->Task->generateList());
				if (empty($this->data['Task']['Task'])) { $this->data['Task']['Task'] = null; }
				$this->set('selectedTasks', $this->data['Task']['Task']);
			}
		}
	}

	function edit($id = null) {
		if (empty($this->data)) {
			if (!$id) {
				$this->Session->setFlash('Invalid id for Tag');
				$this->redirect('/tags/index');
			}
			$this->data = $this->Tag->read(null, $id);
			$this->set('tasks', $this->Tag->Task->generateList());
			if (empty($this->data['Task'])) { $this->data['Task'] = null; }
			$this->set('selectedTasks', $this->_selectedArray($this->data['Task']));
		} else {
			$this->cleanUpFields();
			if ($this->Tag->save($this->data)) {
				$this->Session->setFlash('The Tag has been saved');
				$this->redirect('/tags/index');
			} else {
				$this->Session->setFlash('Please correct errors below.');
				$this->set('tasks', $this->Tag->Task->generateList());
				if (empty($this->data['Task']['Task'])) { $this->data['Task']['Task'] = null; }
				$this->set('selectedTasks', $this->data['Task']['Task']);
			}
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Invalid id for Tag');
			$this->redirect('/tags/index');
		}
		if ($this->Tag->del($id)) {
			$this->Session->setFlash('The Tag deleted: id '.$id.'');
			$this->redirect('/tags/index');
		}
	}

}
?>